<template>
  <div class='container'>
    <el-dialog :title="dialogTitle" :visible="addArticleDialog" @close="closeDialog" @open="openDialog" width="30%">
      <el-form :model="formData" label-width="80px" :rules="formRules" ref="form">
        <el-form-item label="所属学科" v-if="!subject.id">
          <el-select v-model="formData.subjectID" placeholder="请选择" style="width:80%">
            <el-option :label="item.label" :value="item.value" v-for="item in subjectOptions" :key="item.value" ></el-option>
            <el-option label="区域二" value="beijing"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="标签名称" prop="tagName" >
          <el-input v-model="formData.tagName" placeholder="请输入标签名称" style="width:80%"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="closeDialog">取 消</el-button>
        <el-button type="primary" @click="confirm">确 定</el-button>
      </div>

    </el-dialog>
  </div>
</template>

<script>
import { add, update } from '@/api/hmmm/tags.js'
import { simple as subjectList } from '@/api/hmmm/subjects'
export default {
  props: {
    addArticleDialog: {
      type: Boolean,
      required: true
    },
    // 从学科录入进入时，携带的id和name
    subject: {
      type: Object,
      required: true
    }
  },
  data () {
    return {
      dialogTitle: '新增文章',
      subjectOptions: [],
      formData: {
        subjectID: '',
        tagName: ''
      },
      formRules: {
        // subjectID: [
        //   { type: 'number', message: '请选择所属学科', tirgger: 'blur' }
        // ],
        tagName: [
          { required: true, message: '请输入目录名称', tirgger: 'blur' }
        ]
      }
    }
  },
  async created () {
    // 学科下拉选项
    const { data: subjectArr } = await subjectList()
    // console.log(subjectArr)
    this.subjectOptions = subjectArr
  },
  methods: {
    openDialog () {
      // 清除校验效果
      this.$nextTick(() => {
        this.$refs.form.resetFields() // clearValidate()
      })
    },
    closeDialog () {
      this.$emit('update:addArticleDialog', false)
      this.formData = {
        subjectID: '',
        tagName: ''
      }
    },
    // 确认按钮
    async confirm () {
      this.$refs.form.validate(async boo => {
        if (boo) {
          // 提交
          if (!this.formData.id) {
            // 新增
            if (this.subject.id) {
              this.formData.subjectID = +this.subject.id
            }
            const res = await add(this.formData)
            console.log(res)
          } else {
            // 修改
            await update(this.formData)
          }
          this.$message.success(`${this.dialogTitle}操作成功`)
          this.$emit('uploadData')
          this.closeDialog()
        }
      })
    }
  }
}
</script>

<style scoped lang='less'>

</style>
